#!/usr/bin/env python
import os, os.path as path

"""
A simple script that counts the lines in Java source code. =)
Written by MH.
"""

def countLines(file):
	count = 0
	f = open(file, 'rt')
	try:
		for line in f: count += 1
	finally:
		f.close()
	print '%30s: %5d lines.' % (path.basename(file), count)
	return count


def traverseDir(dir):
	print '\nTraversing directory',path.basename(dir),'...'
	files = os.listdir(dir)
	lines = 0
	
	subdirs = []
	countfiles = 0
	for f in files:
		if f.startswith('.'): continue
		realdir = path.join(dir, f)
		if path.isdir(realdir):
			subdirs.append(realdir)
		elif f.endswith('.java'):
			lines += countLines(realdir)
			countfiles += 1
			
	for dir in subdirs:
		newlines, newfiles = traverseDir(dir)
		lines += newlines
		countfiles += newfiles

	return lines, countfiles

if __name__=="__main__":
	print 'Counting Java source code lines...'
	lineCount = fileCount = 0
	lineCount, fileCount = traverseDir(path.join(os.getcwd(), 'src'))

	print '===================================================='
	print 'TOTAL: %d lines in %d .java-files.' % (lineCount, fileCount)

